package com.example.home.findWork;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.findWork.fragment.HistoryFragment;
import com.example.home.findWork.fragment.OnlyMeFragment;
import com.example.home.findWork.fragment.WorkFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class FindWordMainActivity extends AppCompatActivity {
    private BottomNavigationView botview;
    private ImageView back;
    private WorkFragment workFragment;
    private OnlyMeFragment onlyMeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_word_main);

        // 注册广播接收器
        registerReceiver(new WordReceiven(), new IntentFilter("bov_action4"));

        botview = findViewById(R.id.botview);
        back = findViewById(R.id.back);
        HistoryFragment historyFragment = new HistoryFragment();
        onlyMeFragment = new OnlyMeFragment();
        workFragment = new WorkFragment();

        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, workFragment).commit();
        botview.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.work:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, workFragment).commit();
                        break;
                    case R.id.history:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, historyFragment).commit();
                        break;
                    case R.id.onlyMe:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, onlyMeFragment).commit();
                        break;
                }
                return true;
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FindWordMainActivity.this.finish();
            }
        });
    }
    // 广播接收器
    public class WordReceiven extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("bov_action4".equals(action)) {
                // 获取从广播中传递的 fragmentToLoad 参数
                String fragmentToLoad = intent.getStringExtra("fragmentToLoad");

                // 根据参数判断要加载的 Fragment
                if ("OnlyMeFragment".equals(fragmentToLoad)) {
                    // 使用 commitAllowingStateLoss 代替 commit
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            // 示例代码，用于在 FindWordMainActivity 中显示 OnlyMeFragment
                            getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, onlyMeFragment).commitAllowingStateLoss();
                        }
                    });
                }
            }
        }
    }



}